package com.qianbao.finance.sync.modules.elasticsearch.controller;

import com.alibaba.fastjson.JSON;
import com.qianbao.finance.sync.modules.elasticsearch.model.response.Response;
import com.qianbao.finance.sync.modules.elasticsearch.service.MappingService;
import com.qianbao.finance.sync.modules.elasticsearch.util.JsonUtil;
import com.qianbao.finance.sync.modules.sys.controller.AbstractController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author：zgh
 * @DATE: 2018/6/25 16:07
 * @version: 0.0.1
 * @Description:
 */
@RestController
@RequestMapping("/es/accuratelySync")
public class AccuratelySyncController  extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(AccuratelySyncController.class);
    @Resource
    private MappingService mappingService;
    /**
     * 删除配置相关的es中index
     * @return
     */
    @RequestMapping("/removeEsByDataBase")
    public Response removeEsByDataBase(@RequestBody String databaseName){
        logger.info("begin remove all index");
        databaseName = JSON.parseObject(databaseName).getString("dataBaseName");

        Response response = Response.success(mappingService.removeEsByDataBase(databaseName));
        logger.info("end of remove");
        return response;
    }

    /**
     * 在es里边创建index及mapping信息
     * @param databaseName
     * @return
     */
    @RequestMapping("/initMappingByDataBase")
    public Response initMappingByDataBase(@RequestBody String databaseName){
        logger.debug("request_info: " + databaseName);
        databaseName = JSON.parseObject(databaseName).getString("dataBaseName");
        Response response = Response.success(mappingService.initMappingByDataBase(databaseName));
        logger.debug("response_info: " + JsonUtil.toJson(response));
        return response;
    }

    /**
     * 通过配置信息，全库同步数据
     */
    @RequestMapping("/byConfigByDataBase")
    public String byConfigByDataBase(@RequestBody String databaseName) {
        logger.debug("request_info: " + databaseName);
        databaseName = JSON.parseObject(databaseName).getString("dataBaseName");
        String response = Response.success(mappingService.byConfigByDataBase(databaseName)).toString();
        logger.debug("response_info: " + JsonUtil.toJson(response));
        return response;
    }

}
